/*
    Università degli Studi di Trento
    Facoltà di Scienze MM.FF.NN.
    Corso di Laurea Triennale in Informatica
    A.A. 2011/2012
    Progetto per il corso di Ingegneria del Software
    Autori:
    Bertoldi Lorenzo 155863
    Cattin Cosso Manuel 146607
    Parrello Nicola 145610
    Meneghini Nicola 145961
    Vinciguerra Stefano 145962
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generatoreCertificati;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;

import com.itextpdf.text.Document;

import com.itextpdf.text.Element;

import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;

import com.itextpdf.text.pdf.ColumnText;

import com.itextpdf.text.pdf.PdfAction;

import com.itextpdf.text.pdf.PdfContentByte;

import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import com.itextpdf.text.pdf.PdfPageEventHelper;

import com.itextpdf.text.pdf.PdfWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.GregorianCalendar;

public class HeaderFooter extends PdfPageEventHelper {

  protected Paragraph footer;
  protected PdfPTable header;
  protected Document dc;
  private String indirizzo;
  
  public HeaderFooter(Document dc,String indirizzo,int tipoCert){
    this.dc=dc;
    this.indirizzo=indirizzo;//../../../ 3 cartelle
    try{
      setHeader();
      if(tipoCert!=0){
        setFooter();
      }
    }catch(Exception ex){
      System.err.println(ex);
    }
  }

  public void setFooter() throws BadElementException, MalformedURLException, IOException {
    Font fontNote = new Font (Font.FontFamily.TIMES_ROMAN,10,Font.ITALIC,BaseColor.BLACK); 
            String stringa="\n\nIl presente certificato non puo' essere prodotto "
                    + "agli organi della pubblica amministrazione o ai privati"
                    + "gestori di pubblici servizi.\n"
                    + "This certificate may not be issued, or otherwise made "
                    + "available, to any public administrator or private "
                    + "organizations operating in the italian public services"
                    + "(in accordance with article 15 of italian Law no. 183/2011)\n";
                footer=new Paragraph(stringa,fontNote);
  }
  
    @Override
 public void onEndPage(PdfWriter writer,Document document){
    try{
      if(dc.isOpen()){
        dc.add(footer);
      }
    }catch(Exception ex){
      System.err.println(ex);
    }
 }
 
 
    @Override
  public void onStartPage(PdfWriter writer,Document document){
    try{
      if(dc.isOpen()){
        dc.add(header);
      }
    }catch(Exception ex){
      System.err.println(ex);
    }
  }
   
  public void setHeader()throws Exception{
    header = new PdfPTable(2);
    header.setWidthPercentage(100);
    header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    header.getDefaultCell().setBorderColor(BaseColor.WHITE);
    String indImg=indirizzo+"images/logo_unitn.jpg";
    Image image = com.itextpdf.text.Image.getInstance(indImg);
    image.setAlignment(Image.LEFT | Image.TEXTWRAP); 
    header.addCell(image);
    
    PdfPCell cellTesto=new PdfPCell(new Paragraph("AREA DIDATTICA E SERVIZI AGLI STUDENTI\n\n"
          + "SERVIZIO ON­LINE AGLI STUDENTI"));
    cellTesto.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    cellTesto.setBorderColor(BaseColor.WHITE);
    cellTesto.setPaddingTop(20);
    cellTesto.setPaddingBottom(50);
    
    header.addCell(cellTesto);
    header.setHorizontalAlignment(Element.ALIGN_CENTER);
  }

}